import 'package:flutter/material.dart';
import '../../helper/channelHelper.dart';
import '../../helper/uidata.dart';
import '../../model/cleancar.dart';
import '../../model/cleanobjects.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';

class CleanAddPage extends StatefulWidget {
  @override
  CleanAddPageState createState() => new CleanAddPageState();
}

class CleanAddPageState extends State<CleanAddPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final String now = Helper.DateTimeNowString();
  late List<CleanCar> carList;
  bool ifLoaded = false;
  CleanCar carIndex =
      CleanCar(0, 'CarCode', 0, 'UserName', 'Password', 'Location');

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }

  loadData() async {
    carList = await CleanCar.GetAllList();
    carIndex = carList.first;

    ifLoaded = true;
    this.setState(() => {});
  }

  List<DropdownMenuItem> generateItemList() {
    List<DropdownMenuItem> items = [];

    for (var a in carList) {
      var item1 = new DropdownMenuItem(
          key: Key(a.CarCode), value: a, child: new Text(a.CarCode));
      items.add(item1);
    }
    return items;
  }

  getSelectButton() {
    print(carList);
    if (carList.isEmpty) return Container();

    return DropdownButtonHideUnderline(
      child: DropdownButton(
        hint: Text('               '),
        value: carIndex,
        items: generateItemList(),
        onChanged: (u1) {
          setState(() {
            carIndex = u1;
            c1.text = carIndex.CarCode;
            c2.text = carIndex.Volume.toString();
            c3.text = carIndex.UserName;
            c4.text = carIndex.Password;
            c5.text = carIndex.Location;
          });
        },
      ),
    );
  }

  upload(BuildContext context) async {
    var s1 = new CleanObjects(
        0,
        Helper.GetGuid(),
        now,
        0,
        c1.text,
        double.parse(c2.text),
        c3.text,
        c4.text,
        c5.text,
        false,
        "",
        now,
        "",
        now,
        "",
        "",
        now,
        "",
        false,
        "",
        now,
        false,
        "",
        now,
        UIData.CurrentUser!.Guid);
    await CleanObjects.Add(s1);
    ChannelHelper.showToast("创建成功！");
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("漂浮物转运工作单"),
      ),
      body: ifLoaded ? CleanAddBody(context) : Container(),
    );
  }

  CleanAddBody(BuildContext context) => SingleChildScrollView(
        child: CleanAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list2.add(ListTile(
      title: Text("选择车辆"),
      trailing: getSelectButton(),
    ));
    list1.add(InputWidget.inCard2(list2));

    list3.add(InputWidget.getInput("车牌号码", c1));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("运载量", c2));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("司机姓名", c3));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("联系电话", c4));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("运输地点", c5));
    list1.add(InputWidget.inCard2(list3));
    list5.add(InputWidget.getButton("创建工作单", () => upload(context)));

    list1.add(InputWidget.inCard2(list5));

    return list1;
  }

  CleanAddFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
